using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace GameUI {
	public class WeaponBaseImageController:
	UIElementControllerBase,
	IInGameInfo,
	ITransferrable {

		InventoryWeaponController weaponController;

		void Start() {
			weaponController=GetComponentInParent<InventoryWeaponController>();
		}

		protected override void OnPointerRelease() {
			ITransferrable other = InventoryUIController.instance.mouseItem;
			if(other==null) return;

			if(other.contentItem is Inventory.ItemUpgradePoint&&!weaponController.boundWeapon.usingDefaultWeapon) {
				Inventory.ItemUpgradePoint otherAsUpgradePoint = other.contentItem as Inventory.ItemUpgradePoint;
				if(otherAsUpgradePoint.level>weaponController.boundWeapon.weaponInstance.weaponLevel&&other.TryTake()) {
					otherAsUpgradePoint.ApplyUpgrade(weaponController.boundWeapon.weaponInstance);
				}
			}

			UpgradeInstance upgrade = TransferrableUtils.GetUpgradeFromTransferrableContent(other.contentItem);
			if(upgrade!=null&&weaponController.boundWeapon.weaponInstance.CanAddUpgrade(upgrade)) {
				if(other.TryTake()) weaponController.boundWeapon.weaponInstance.TryAddUpgrade(upgrade,null);
			}

			if(other==null) return;
			if(!DoAccept(other.contentItem)) return;
			if(!other.DoAccept(contentItem)) return;

			object temp = contentItem;
			if(!TryAccept(other.contentItem)) throw new System.Exception("PASS ACCEPT TEST BUT CANNOT ACCEPT");
			if(!other.TryAccept(temp)) throw new System.Exception("PASS ACCEPT TEST BUT CANNOT ACCEPT");

		}
		public override void OnPointerDown(PointerEventData eventData) {
			InventoryUIController.instance.TransferrableClick(this);
		}

		public override void OnPointerEnter(PointerEventData eventData) {
			base.OnPointerEnter(eventData);
			InventoryMouseObject.instance.mouseOverObject=this;
		}
		public override void OnPointerExit(PointerEventData eventData) {
			base.OnPointerExit(eventData);
			if(InventoryMouseObject.instance.mouseOverObject==this as IInGameInfo)
				InventoryMouseObject.instance.mouseOverObject=null;
		}

		#region ITransferrable
		public bool canTake => !weaponController.boundWeapon.usingDefaultWeapon;
		public new string name => weaponController.boundWeapon.weaponInstance.name;
		public string descr => weaponController.boundWeapon.weaponInstance.descr;

		public bool Choose() {
			return canTake;
		}
		public bool TryTake() {
			if(!canTake) return false;
			weaponController.boundWeapon.weaponInstance=weaponController.boundWeapon.defaultWeaponInstance;
			return true;
		}
		public void Return() { }

		public bool DoAccept(object content) {
			if(content is WeaponInstance) return true;
			return false;
		}
		public bool TryAccept(object content) {
			if(!DoAccept(content)) return false;
			WeaponInstance newWeapon = content as WeaponInstance;
			if(newWeapon!=null) weaponController.boundWeapon.weaponInstance=newWeapon;
			return true;
		}

		public Sprite itemSprite => weaponController.boundWeapon.weaponInstance.weaponData.sprite;

		public object contentItem => weaponController.boundWeapon.weaponInstance;

		#endregion
	}
}